package com.itsv.cyzx.adapter;

import java.util.List;

import com.itsv.cyzx.bean.ContactsBean;
import com.itsv.cyzx.util.LoadImageAysnc;
import com.itsv.cyzx.util.LoadImageAysnc.ImageCallBack;
import com.itsv.xhyy.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;

public class ContactsJiebieBaseAdapter extends BaseExpandableListAdapter {
	private Context context;
	private LayoutInflater mInflater;
	private String[] mGroupStrings = null;
	private List<List<ContactsBean>> mData = null;
	private LoadImageAysnc loadImageAysnc;

	private ExpandableListView listView;

	public ContactsJiebieBaseAdapter(Context context,
			List<List<ContactsBean>> list, String[] mGroupStrings,
			ExpandableListView listView) {
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mData = list;
		this.mGroupStrings = mGroupStrings;
		this.listView = listView;
		loadImageAysnc = new LoadImageAysnc(context);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mData.get(groupPosition).size();
	}

	@Override
	public List<ContactsBean> getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mData.get(groupPosition);
	}

	@Override
	public ContactsBean getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mData.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.group_item_layout, null);
		}
		GroupViewHolder holder = new GroupViewHolder();
		holder.mGroupName = (TextView) convertView
				.findViewById(R.id.group_name);
		holder.mGroupName.setText(mGroupStrings[groupPosition]);
		holder.mGroupCount = (TextView) convertView
				.findViewById(R.id.group_count);
		holder.mGroupCount.setText("[" + mData.get(groupPosition).size() + "]");
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.from(context).inflate(
					R.layout.contacts_listitem, null);
			holder = new ChildViewHolder();
			holder.huikorLayout = convertView.findViewById(R.id.huikor_layout);
			holder.mImageButton = (ImageButton) convertView
					.findViewById(R.id.huikorHead);
			holder.fenleiLayout = convertView.findViewById(R.id.fenlei_layout);
			holder.fenleiTextView = (TextView) convertView
					.findViewById(R.id.fenlei_textView);
			holder.mNameTextView = (TextView) convertView
					.findViewById(R.id.huikorName);
			holder.fenleiLayout.setVisibility(View.GONE);
			convertView.setTag(holder);
		} else {
			holder = (ChildViewHolder) convertView.getTag();
		}
		final ContactsBean bean = getChild(groupPosition, childPosition);

		holder.mImageButton.setTag(bean.getId());
		Drawable drawable = loadImageAysnc.loadImage(bean.getId(),
				bean.getPicture(), bean.getId() + ".jpg", new ImageCallBack() {

					@Override
					public void imageLoaded(Drawable drawable, String iconSrc) {
						ImageButton imageViewTag = (ImageButton) listView
								.findViewWithTag(bean.getId());
						if (imageViewTag != null) {
							imageViewTag.setBackgroundDrawable(drawable);
						}

					}
				});

		if (drawable == null) {
			holder.mImageButton
					.setBackgroundResource(R.drawable.personal_card_avatar_defult);
		} else {
			holder.mImageButton.setBackgroundDrawable(drawable);
		}
		holder.mNameTextView.setText(bean.getName());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	private class GroupViewHolder {
		TextView mGroupName;
		TextView mGroupCount;
	}

	class ChildViewHolder {
		private View huikorLayout;
		private ImageButton mImageButton;
		private View fenleiLayout;
		private TextView fenleiTextView;
		private TextView mNameTextView;
	}

}
